import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author 徐志荣
 * @date 2023/2/21 17:18
 */
public class ReflectClassDemo {
    public static void reflectNewInstance() {
        try {
            Class<?> c1 = Class.forName("Student2");
            Student2 student2 = (Student2) c1.newInstance();
            System.out.println("学生对象:"+student2);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }

    }
    public static void reflectPrivateConstructor() {
        try {
            Class<?> c1 = Class.forName("Student2");
            //获取所有的构造方法
            Constructor<?> constructor = c1.getDeclaredConstructor(String.class,int.class);
            constructor.setAccessible(true);
            Student2 student2 = (Student2)constructor.newInstance("jiajia",20);
            System.out.println(student2);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

    }

    public static void reflectPrivateField() {
        try {
            Class<?> c1 = Class.forName("Student2");
            Field field = c1.getDeclaredField("name");
            field.setAccessible(true);

            Student2 student2 = (Student2) c1.newInstance();
            field.set(student2,"猪猪侠");
            System.out.println(student2);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }  catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
    public static void reflectPrivateMethod() {
        try {
            Class<?> c1 = Class.forName("Student2");
           Method method =  c1.getDeclaredMethod("function",String.class);
            method.setAccessible(true);

            Student2 student2 = (Student2) c1.newInstance();
            method.invoke(student2,"猪猪家!!!");
            //System.out.println(student2);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }  catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        //reflectNewInstance();
        //reflectPrivateConstructor();
        //reflectPrivateField();
        reflectPrivateMethod();
    }

}
